// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import '../static/css/element-variables.scss'
import '../static/iconfont/iconfont.css'
import App from './App'
import router from './router'
import axios from 'axios'


// import echarts from 'echarts'


Vue.use(ElementUI)

Vue.config.productionTip = false

let DomianUrl = {
  'production': "https://www.concare.cn/lms-interface-test/",
  'test': "https://www.concare.cn/lms-interface-test/",
  'development': "https://www.concare.cn/lms-interface-test/",
}
console.log(process.env);
axios.defaults.withCredentials = true;
axios.defaults.timeout = 7000;
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8;';
axios.defaults.baseURL = DomianUrl[process.env.NODE_ENV];

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  config.headers['Authorization'] = localStorage.getItem('token')
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response;
}, function (error) {
  // 对响应错误做点什么
  if (error && error.response) {
    switch (error.response.status) {
      case 400:
        error.message = '错误请求'
        break;
      case 401:
        error.message = '未授权，请重新登录'
        break;
      case 403:
        error.message = '拒绝访问'
        break;
      case 404:
        error.message = '请求错误,未找到该资源'
        break;
      case 405:
        error.message = '请求方法未允许'
        break;
      case 408:
        error.message = '请求超时'
        break;
      case 500:
        error.message = '服务器端出错'
        break;
      default:
        error.message = `连接错误`
    }
    error.message += error.response.status
  } else {
    error.message = "连接到服务器失败"
  }
  return Promise.reject(error);
});

// 挂载全局post请求
Vue.prototype.post = function (url, param, callback) {
  if (!url) {
    this.$message.error("url为空");
    return
  }
  let loading = this.$loading({
    lock: true,
    text: "加载中",
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.5)"
  });
  return axios({
    method: "POST",
    url: url,
    data: param
  }).then(res => {
    loading.close()
    if (!res) {
      return
    }
    if (res.data.code == 401) {
      self.location.href = window.location.protocol + "//" + window.location.host + "";
      this.$message.error(res.data.msg + '401')
      return
    }
    if (callback) {
      if (res.data.success) {
        callback(res.data)
      } else {
        this.$message.error(res.data.msg)
      }
    }
  }).catch(error => {
    this.$message.error(error.message)
    loading.close()
  })
}

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
